const app = getApp();
import { getVideoList, addBrowse } from '../../api/videoApi';
import { listData } from '../../api/dict/data.js'
import config from '../../config.js';
// import DefaultImg from '../../images/video-img/sp1.png'

Page({
  data: {
    isAuthorized: false,
    categories: [],
    activeCategory: '全部',
    DefaultImg: '../../images/video-img/sp1.png',
    videosRaw: [
      { id: 1, title: '如何按时按量安全用药', cover: '../../images/video-img/sp1.png', views: '2000+', uploadTime: '2025-05-28 09:30', category: 'dm', tags: ['用药提醒', '基础'] },
      { id: 2, title: '阿司匹林服用注意事项', cover: '../../images/video-img/sp2.png', views: '2000+', uploadTime: '2025-05-27 18:20', category: 'tb', tags: ['注意事项', '安全'] },
      { id: 3, title: '高血压患者用药指南', cover: '../../images/video-img/sp3.png', views: '2000+', uploadTime: '2025-05-26 14:05', category: 'dm', tags: ['指南', '控糖'] },
      { id: 4, title: '糖尿病控糖与用药', cover: '../../images/video-img/sp4.png', views: '2000+', uploadTime: '2023-05-25 12:10', category: 'dm', tags: ['控糖', '饮食'] }
    ],
    videos: []
  },

  onLoad() {
    this.checkAuthStatus();
    this.applyFilter()
    this.getCategoryList()
    this.getList()
  },

  onShow() {
    this.checkAuthStatus();
  },

  // 检查授权状态
  checkAuthStatus: function () {
    const isAuthorized = app.globalData.isAuthorized;
    this.setData({
      isAuthorized
    });
  },

  // 处理用户登录
  handleUserLogin: function () {
    wx.showLoading({
      title: '登录中...'
    });

    app.userLogin().then((userInfo) => {
      wx.hideLoading();
      this.setData({
        isAuthorized: true
      });
      wx.showToast({
        title: '登录成功',
        icon: 'success'
      });
    }).catch((err) => {
      wx.hideLoading();
      wx.showToast({
        title: '登录失败',
        icon: 'error'
      });
      console.error('登录失败:', err);
    });
  },

  async getCategoryList() {
    const params = {
      pageNum: 1,
      pageSize: 9999,
      dictType: 'video_tags'
    }
    const res = await listData(params)
    const categories = res.map(v => ({ key: v.dictValue, name: v.dictLabel }))
    categories.unshift({ key: 'all', name: '全部' })
    this.setData({ categories })
    console.log(res, 222)
  },

  async getList() {
    const params = {
      pageNo: 1,
      pageSize: 100,
    }
    if (this.data.activeCategory !== '全部') params.type = this.data.activeCategory
    const res = await getVideoList(params)
    this.setData({ videos: res.map(v => (
      {
        ...v,
        tags: v.tag.split(','),
        cover: v.coverPath ? `${config.originUrl}/file/${v.coverPath}` : this.data.DefaultImg,
        videoUrl: `${config.originUrl}/file/${v.path}`
      }
    )) })
  },

  // 浏览次数
  async addBrowseFn(id) {
    await addBrowse({ id })
  },

  onCoverTap(e) {
    const { id, path } = e.currentTarget.dataset
    if (!path) return wx.showToast({ title: '暂无视频', icon: 'none' })
    const arr = this.data.videos.map(v => {
      if (v.id === id) {
        v.showVideo = true
      } else v.showVideo = false
      return v
    })
    console.log(id, 22)
    console.log(arr, 111)
    this.setData({ videos: arr }, () => {
      this.addBrowseFn(id)
    })
  },

  switchCategory(e) {
    const label = e.currentTarget.dataset.label
    this.setData({ activeCategory: label }, () => {
      this.getList()
    })
    // this.applyFilter()
  },

  applyFilter() {
    const list = this.data.videosRaw.filter(v => this.data.activeCategory === 'all' || v.category === this.data.activeCategory)
    this.setData({ videos: list })
  }
})

